package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.DTO.angency.FleetDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.PdFleet;
import com.itheima.pinda.mapper.PdFleetMapper;
import com.itheima.pinda.service.PdFleetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdFleetServiceImpl implements PdFleetService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdFleetMapper fleetMapper;

    @Override
    public FleetDto insert(FleetDto fleetDto) {
        PdFleet fleet = new PdFleet();
        BeanUtils.copyProperties(fleetDto,fleet);

        if (StringUtils.isEmpty(fleet.getId())) {
            // 新增
            fleet.setId(idGenerator.nextId(fleet) +"");
            fleetMapper.insert(fleet);
            fleetDto.setId(fleet.getId());
        }else {
            // 修改
            fleetMapper.updateById(fleet);
        }
        return fleetDto;
    }

    @Override
    public FleetDto selectById(String id) {
        PdFleet fleet = fleetMapper.selectById(id);
        FleetDto fleetDto = new FleetDto();
        BeanUtils.copyProperties(fleet,fleetDto);
        return fleetDto;
    }

    @Override
    public IPage selectByPage(Integer pageNum, Integer pageSize, String name, String fleetNumber, String manager) {
        Page<PdFleet> page = new Page<>(pageNum, pageSize);
        Page<PdFleet> selectPage = fleetMapper.selectPage(page,
                new LambdaQueryWrapper<PdFleet>()
                        .eq(!StringUtils.isEmpty(name), PdFleet::getName, name)
                        .eq(!StringUtils.isEmpty(fleetNumber), PdFleet::getFleetNumber, fleetNumber)
                        .eq(!StringUtils.isEmpty(manager), PdFleet::getManager, manager));

        ArrayList<FleetDto> fleetDtos = new ArrayList<>();
        List<PdFleet> records = selectPage.getRecords();
        List<FleetDto> dtoList = records.stream().map(pdFleet -> {
            FleetDto fleetDto = new FleetDto();
            BeanUtils.copyProperties(pdFleet, fleetDto);
            return fleetDto;
        }).collect(Collectors.toList());

        return new Page<FleetDto>().setRecords(dtoList);
    }

    @Override
    public void deleteById(List<String> ids) {
        fleetMapper.update(null,
                new LambdaUpdateWrapper<PdFleet>()
                        .set(PdFleet::getStatus, Constant.DATA_DISABLE_STATUS)
                        .in(PdFleet::getId,ids));

    }
}
